<%@page import="java.sql.Statement"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%
            String data = "";
       String sql = "";
       String json = "";
       int errorcode = 0;
       ResultSet res = null;
       model.ConnectionDB connection = new model.ConnectionDB();

        if(request.getParameter("Action").equals("Add")){
            sql = "SELECT addroutes(" + request.getParameter("ID") + "," + request.getParameter("COST") + ",'" + request.getParameter("SALIDA") + "'," + request.getParameter("DURACION") + ",'" + request.getParameter("ORIGEN")+"','" + request.getParameter("DESTINO")+"')";
          try {
                            connection.connect();
                            res=connection.consult(sql);

                            connection.getCon().close();

                            //if(res.next())
                               // out.print(sql);
                              //data="{'ans' : " + res.getInt(1) + "}";//Add  "'ori' :'" + res.getString(2) + "', ";
                    } catch (Exception e) {
                           errorcode =-5;
                    }
            }
        else if(request.getParameter("Action").equals("Update")){
            sql = "SELECT updateroutes(" + request.getParameter("ID") + ",'" + request.getParameter("COST") + "','" + request.getParameter("SALIDA") + "','" + request.getParameter("DURACION") + "','" + request.getParameter("ORIGEN")+"','" + request.getParameter("DESTINO")+"')";
         try {
                            connection.connect();
                            res=connection.consult(sql);

                            connection.getCon().close();

                            //if(res.next())
                               // out.print(sql);
                              //data="{'ans' : " + res.getInt(1) + "}";//Add  "'ori' :'" + res.getString(2) + "', ";
                    } catch (Exception e) {
                           errorcode =-5;
                    }
            }
       /* else if(request.getParameter("Action").equals("Delete")){
            sql = "SELECT DeleteRoute(" + request.getParameter("ORIGIN")+ ",'" + request.getParameter("DESTINATION")+ "')";

            try {
                            connection.connect();
                            res=connection.consult(sql);
                            connection.getCon().close();
                            if(res.next())
                                data="{'ans' : " + res.getInt(1) + "}";//Add
                    } catch (Exception e) {
                            if(res.next())
                                data="{'ans' : " + res.getInt(1) + "}";//Add
                    } catch (Exception e) {
                            errorcode = -7;
                            connection.getCon().close();
                    }
            }
 
        */ else if (request.getParameter("Action").equals("Vehicles")){
                sql ="SELECT * FROM seekidvehicles();";
                 try {
                connection.connect();
                res = connection.consult(sql);
                connection.getCon().close();

                while (res.next()) {
            //if(res.next()){
            //HttpSession sessionUser=request.getSession();//Create new Session
            //sessionUser.setAttribute("result",res);
                    if (!data.equals("")) {
                        data += ",";
                    }
                    data += '{';
                    data += "'i' :" + res.getInt(2);
                    //data += "'state' :'" + res.getString("city_state") + "', ";
                    //data += "'idCity': " + res.getInt("city_id") + ", ";
                    data += '}';
                }
            }catch(Exception e) {
                out.print(e.getMessage());
                out.print("{'errorcode' : -1}"); //
                connection.getCon().close();
            }
            }

            else if (request.getParameter("Action").equals("Origin")){
                sql ="SELECT * FROM seekcities();";
                       

                         try {
                connection.connect();
                res = connection.consult(sql);
                connection.getCon().close();

                while (res.next()) {
            //if(res.next()){
            //HttpSession sessionUser=request.getSession();//Create new Session
            //sessionUser.setAttribute("result",res);
                    if (!data.equals("")) {
                        data += ",";
                    }
                    data += '{';
                    data += "'ori' :'" + res.getString(2) + "', ";
                    //data += "'state' :'" + res.getString("city_state") + "', ";
                    //data += "'idCity': " + res.getInt("city_id") + ", ";
                    data += '}';
                }
            }catch(Exception e) {
                out.print(e.getMessage());
                out.print("{'errorcode' : -1}"); //
                connection.getCon().close();
            }
            }

            else if (request.getParameter("Action").equals("Destination")){
                sql ="SELECT * FROM seekcities();";
                       


                         try {
                connection.connect();
                res = connection.consult(sql);
                connection.getCon().close();

                while (res.next()) {
            //if(res.next()){
            //HttpSession sessionUser=request.getSession();//Create new Session
            //sessionUser.setAttribute("result",res);
                    if (!data.equals("")) {
                        data += ",";
                    }
                    data += '{';
                    data += "'des' :'" + res.getString(2) + "', ";
                    //data += "'state' :'" + res.getString("city_state") + "', ";
                    //data += "'idCity': " + res.getInt("city_id") + ", ";
                    data += '}';
                }
            }catch(Exception e) {
                out.print(e.getMessage());
                out.print("{'errorcode' : -1}"); //
                connection.getCon().close();
            }
            }



        json = "{'errorcode' : " + errorcode + ", 'data' : [" + data + "]}";
       out.print(json);
%>